草庐IT

python - 将 int 转换为 ASCII 并返回 Python

全部标签

python - 如何知道远程tcp设备是否关机

在我的GO代码中,我正在建立一个TCP连接,如下所示:conn,err1:=net.Dial("tcp",)iferr1==nil{buf:=make([]byte,256)text,err:=conn.Read(buf[:])iferr==io.EOF{//remoteconnectionclosehandlefmt.Println("connectiongotresetbypeer")panic(err)}}为了模拟另一端,我在另一台计算机上运行一个python脚本,它打开一个套接字并将一些随机数据发送到上面的代码行正在监听的套接字。现在我的问题是,当我通过按ctrl+C杀死这个p

go - 将字符串日期 (Month, Year) 转换为 time.Time

我有一个字符串是:str:="Jan2020"我需要在go中将其转换为time.time格式。请问我该怎么做? 最佳答案 您需要有一个布局字符串来指定如何解析您的字符串。例如:packagemainimport("time""fmt")funcmain(){time,err:=time.Parse("Jan2006","Feb2020")iferr!=nil{panic(err)}fmt.Println(time)}您可能会找到更多关于标准布局的信息here. 关于go-将字符串日期(M

go - 如何将父类型转换为子类型

给定以下类型:type(Parentstruct{namestringsurnamestring}Childstruct{*ParentsportString})...func(p*Parent)GetSport()string{return((*Child)(p)).sport//doesnotwork}如何将*Parent转换为*Child? 最佳答案 func(p*Parent)Convert()*Child{return&Child{p,""}}https://play.golang.org/p/saGvRu_rIk问题是没

json 和类型转换

我正在尝试读取一个看起来像这样的json文件[{"title":"hi","tags":[1,2,3,4,5,6]},{...},{...}]代码是这样的contentdat,err:=ioutil.ReadFile("content.json")check(err)varcontent[]interface{}err=json.Unmarshal(contentdat,&content)check(err)fori,contentItem:=rangecontent{vertedContentItem:=contentItem.(map[string]interface{})cont

go - 将一片字符串转换为一片 float32

在我的程序中,我通过bufio扫描器将一串数字(例如:5443.3-43.2)添加到slice中。然后我想将每个空间的slice拆分成另一个slice以将其转换为float32。这是我所拥有的:varnewSlice[]float32sliceScan=scanner.Text()s:=strings.Split(sliceScan,"")fori:=0;i当我运行它时,我得到了这个错误:syntaxerror:unexpectedsattheendofstatement 最佳答案 您可以使用strconv.ParseFloat:v

json - Golang json解码返回空

这个问题在这里已经有了答案:JSONanddealingwithunexportedfields(2个答案)关闭4年前。有人可以向我解释为什么这段代码无法正确解码json吗:packagemainimport("fmt""os""log""encoding/json")typeConfigstruct{mongoConnectionStringstring`json:"database"`Elasticstruct{mainstring`json:"main"`logstring`json:"log"`}`json:"elastic"`logFilePathstring`json:"l

parsing - 将 String 转换为完全相同的 Int

如何在不从开头删除0数字前缀的情况下将字符串转换为整数。像这样的用例我有一个像“0093”这样的字符串,我想将与0093相同的整数转换为整数。我尝试strconv但问题是这个包在转换后从0093中删除了00前缀。谁能有更好的解决方案来解决这个问题。s:="0093"ifi,err:=strconv.Atoi(s);err==nil{fmt.Printf("i=%d,type:%T\n",i,i)}输出是93,但我想要int类型的确切0093。 最佳答案 来自fmt包的文档:Widthisspecifiedbyanoptionalde

pointers - 您可以将多个值中的部分或全部作为函数的指针返回吗?

在使用Go时,我遇到了各种错误,试图在返回另一个值的同时返回一个字符串作为指针。像这样的东西(请原谅这不是运行代码,我只是写它来了解我想做什么,因为我不知道如何让它工作):funcA(sstring)*string,int{//Stuffreturn&a,b}*c,d:=A("Hithere.")当我尝试各种组合say时,将字符串(var.a)作为指针返回时,我遇到了各种错误。这很简单,有许多返回单个变量的示例,但我不确定是否可以使用多个返回值。抱歉,如果这看起来是一个非常基本的问题,我仍然在思考Go。 最佳答案 如前所述here在

dictionary - 无法将 map 对象转换为 JSON 对象

我正在用Go语言编写一些代码。我是Go语言的新手,我被困在一个地方。我现在有一个看起来像这样的map对象count:=map[string]int{}count["Kitchen"]=1count["Electronics"]=1theoutputlookslikethis:map[Electronics:1Kitchen:1]现在我在做answer,_:=json.Marshal(count)预期的答案应该是这样的:{"Kitchen":1,"Electronics":1}但它是这样来的:[12334691081019911611411111010599115345849443475

尝试附加相同类型的结构时,Go 返回错误

我在尝试使用追加函数合并两个结构时遇到错误:./test.go:33:18:cannotuseconfigs(typeMapUsers)astypestruct{Userarnstring"yaml:\"userarn\"";Usernamestring"yaml:\"username\"";Groups[]string"yaml:\"groups\""}inappend这是代码。packagemainimport("fmt""gopkg.in/yaml.v2""io/ioutil""os")typeMapUsers[]struct{UserarnstringUsernamestrin